Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JIT: boost inlining when callee unboxes an arg #110596

Merged
merged 2 commits into from
Feb 7, 2025

Conversation

AndyAyersMS
Copy link
Member

Especially so if the caller is passing an exact type. This may lead to the JIT being able to stack allocate the box and promote the underlying payload.

Fixes #104479

Especially so if the caller is passing an exact type. This may lead
to the JIT being able to stack allocate the box and promote the underlying
payload.

Fixes dotnet#104479
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Dec 10, 2024
@AndyAyersMS
Copy link
Member Author

@MihuBot

@AndyAyersMS
Copy link
Member Author

On the example from #104479:

; Method Program:<<Main>$>g__BoxTest|0_0():int (FullOpts)
G_M15828_IG01:  ;; offset=0x0000
       sub      rsp, 40
						;; size=4 bbWeight=1 PerfScore 0.25

G_M15828_IG02:  ;; offset=0x0004
       mov      ecx, 42
       call     [System.Number:Int32ToDecStr(int):System.String]
       mov      rdx, rax
       mov      rcx, 0x7FFC28EF66C0      ; Program:<<Main>$>g__Consume|0_2[System.String](System.String)
       call     [Program:<<Main>$>g__Consume|0_2[System.__Canon](System.__Canon)]
       mov      eax, 42
						;; size=35 bbWeight=1 PerfScore 7.00

G_M15828_IG03:  ;; offset=0x0027
       add      rsp, 40
       ret      
						;; size=5 bbWeight=1 PerfScore 1.25
; Total bytes of code: 4

SPMI sees some diffs and quite a few missed contexts.

MihuBot did not see many diffs, however:

15 total methods with Code Size differences (0 improved, 15 regressed), 232918 unchanged.

So we may need a bespoke SPMI to really evaluate the imapact here.

@AndyAyersMS
Copy link
Member Author

@hez2010 FYI -- seems quite easy to blow code up this way, but perhaps worth considering.

@AndyAyersMS
Copy link
Member Author

@EgorBo FYI

@EgorBo
Copy link
Member

EgorBo commented Feb 7, 2025

@MihuBot

@@ -1716,6 +1724,30 @@ double ExtendedDefaultPolicy::DetermineMultiplier()
JITDUMP("\nPrejit root candidate has arg that feeds a conditional. Multiplier increased to %g.", multiplier);
}

if (m_ArgUnboxExact > 0)
Copy link
Member

@EgorBo EgorBo Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AndyAyersMS I think you can also rely on existing m_ArgIsBoxedAtCallsite here, so if an arg is boxed on the callsite and unboxed in the callee - it should be quite profitable.

UPD: although, probably not - m_ArgIsBoxedAtCallsite alone is not telling which arg exactly is boxed 🙂

@AndyAyersMS
Copy link
Member Author

At least from MihuBot I only see more inlining, and no cases where we removed allocations. But might as well see if the lab spots anything.

@AndyAyersMS AndyAyersMS merged commit 4dcd4d3 into dotnet:main Feb 7, 2025
113 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

A simple method that performs unboxing is considered unprofitable inlinee, preventing box stack allocation
2 participants